<?php
error_reporting(0);
/**
 * GameController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class GpageController extends Sunnet_Controller_Action
{
    var $_arrParam;
    protected $domain = DOMAIN;
    public function init()
    {
        //      Truyền các tham số nhận được ra view
        $this->_arrParam = $this->_request->getParams();
        if (empty($this->_arrParam['page'])) {
            $this->_arrParam['page'] = 1;
        }
        if (empty($this->_arrParam['line_per_page'])) {
            $this->_arrParam['line_per_page'] = 10;
        }

        $this->view->arrParam = $this->_arrParam;
        $this->view->domain = $this->domain;
        $this->view->function = $this;
    }

    public function preDispatch(){
        $template_path = TEMPLATE_PATH . "/wap/default/";
        $this->loadTemplate($template_path,'template.ini','template');
         //set group main
        Zend_Loader::loadClass('Wap_Models_ControllerModel');
        $controllerModel = new Wap_Models_ControllerModel();  

        Zend_Loader::loadClass('Wap_Models_GpageModel');
        $gpageModel = new Wap_Models_GpageModel();       

        $this->view->groupmain_list = $controllerModel->listMenuGroupNote();
        $this->view->gpage_list = $gpageModel->getCategoryGpageID();
        //$this->view->newsHots = $controllerModel->listNewsHotRight();
        $this->view->listBanners = $controllerModel->listBannerHeader();

        //      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        
    }

    public function indexAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');
        //list note group
 
        $params = $this->_request->getParams();

        Zend_Loader::loadClass('Wap_Models_GpageModel');
        $mPost= new Wap_Models_GpageModel(); 
        //load info post
        $post = $mPost->getPageID(ID_GPAGE); 
        $this->view->post = $post;        

        //SEO POST        
        if(!empty($post['seo_title'])) $this->view->headTitle()->headTitle($post['seo_title']);
        else $this->view->headTitle()->headTitle($post['name_note']);
        if(!empty($post['seo_keywords'])) $this->view->headMeta()->appendName('keywords', $post['seo_keywords']);
        if(!empty($post['seo_description'])) $this->view->headMeta()->appendName('description', $post['seo_description']);
    }

    public function postAction (){     
        //list note group

        $params = $this->_request->getParams();

        Zend_Loader::loadClass('Wap_Models_GpageModel');
        $mNote= new Wap_Models_GpageModel();        
        //load info post
        $post = $mNote->getNoteID(intval($params['id']));
        $this->view->post = $post;
        //load tin tuc lien quan
        $releasePost = $mNote->getListNoteNotIdView(intval($params['id']), intval($post['GroupNoteID']));
        $this->view->releasePost = $releasePost;

        //lay category dang view
        $category =  $mNote->getCategoryNote($post['GroupNoteID']);
        $this->view->category = $category;

        //SEO POST        
        if(!empty($post['seo_title'])) $this->view->headTitle()->headTitle($post['seo_title']);
        else $this->view->headTitle()->headTitle($post['name_note']);
        if(!empty($post['seo_keywords'])) $this->view->headMeta()->appendName('keywords', $post['seo_keywords']);
        if(!empty($post['seo_description'])) $this->view->headMeta()->appendName('description', $post['seo_description']);
    }

    public function cateAction (){
        //luu session post status 
 
        //
        $this->_arrParam = $this->_request->getParams();
        //list note group
        if(!isset($this->_arrParam['gid']) or ($this->_arrParam['gid']=="")){
            //list all bai viet
            $gid = 0;
        }else {
            $gid = intval($this->_arrParam['gid']);
        }  

        //lay category dang view
        if($gid!=0){
            Zend_Loader::loadClass('Wap_Models_GpageModel');
            $mNote= new Wap_Models_GpageModel();  
            $category =  $mNote->getCategoryNote($gid);
            $this->view->category = $category;
        }
        $this->view->gid = $gid;
        //count note truyen
        $count_note =  intval($mNote->getCountNoteByCategory($gid));
        $this->view->count_note = $count_note;
    
        //Code phan trang doan nay        
        if(!isset($this->_arrParam['page']) or ($this->_arrParam['page']=="")){
            $page = 1;
        }else {
            $page = intval($this->_arrParam['page']);
        }
        $count_page = ceil($count_note/10);
        $this->view->countpage = $count_page;
        $this->view->page = $page;
        //Zend_debug::dump($count_avatar); die();
        //end phan trang  

        //list category         
        $note_lists = $mNote->getNoteByCategory($gid,$page);
        $this->view->note_lists = $note_lists; 

        //SEO CATEGORY
        $loadCategory = $mNote->getCategoryNoteID($gid);

        if(!empty($loadCategory['seo_title'])) $this->view->headTitle()->headTitle($loadCategory['seo_title']);
        else $this->view->headTitle()->headTitle($loadCategory['name_group']);
        if(!empty($loadCategory['seo_keywords'])) $this->view->headMeta()->appendName('keywords', $loadCategory['seo_keywords']);
        if(!empty($loadCategory['seo_description'])) $this->view->headMeta()->appendName('description', $loadCategory['seo_description']);

    }

    //code date time
    public function convertTimeAgo ($activity_time){       
       $timeinsecond = time() - $activity_time;
       if($timeinsecond >=0){
         $minus  = abs(floor(($timeinsecond%3600)/60));
         $hour   =   abs(round(($timeinsecond-$minus*60)/3600,0));              
          
         $timematch = 0;
         if($timeinsecond<60){
            $timematch = $timeinsecond." giây trước";  
            return $timematch;
         }elseif(60<$timeinsecond && $timeinsecond<3600){
            $timematch = $minus." phút trước";      
            return $timematch;       
         }elseif(3600<=$timeinsecond && $timeinsecond<86400){
            $timematch = $hour." giờ trước"; 
            return $timematch;
         }elseif(86400<=$timeinsecond && $timeinsecond<518400){
            $timematch =  abs(round(($hour/24))) . " ngày trước";
            return $timematch;
         }else {
            $timematch= date('d/m/Y', $activity_time);
            return $timematch;
         }
         return $timematch;
       }
    }
    function cut_substr($str,$n)
    {       
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        $html = substr($str,0,$n);
        $html = substr($html,0,strrpos($html,' '));

        return $html.'...';
    }  

    function cut_substr_strip($str)
    {                   
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        return $str;

    }  

    function getListFollowGpageId($gpageid)
    {                   
        Zend_Loader::loadClass('Wap_Models_GpageModel');
        $gpageModel = new Wap_Models_GpageModel();    

        $list = $gpageModel->getlistgpagebyGroupID($gpageid);
        return $list;
    }  

}
